<template>
  <view class="demo">
    <l-painter
      isCanvasToTempFilePath
      @success="path = $event"
      css="width: 750rpx;"
      hidden
    >
      <l-painter-image
        src="/static/images/1.jpg"
        css="width: 750rpx; height: 1750rpx;"
      />
      <l-painter-qrcode
        text="http://43.138.186.106:85/pages/register"
        css="width: 180rpx; height: 180rpx;position: absolute;top: 1250rpx;background-color: #fff;box-sizing: border-box;padding: 10rpx;border-radius: 10rpx;right: 93rpx;z-index: 50;"
      />

      <l-painter-view
        css="position: absolute;top: 1310rpx; right: 400rpx;z-index: 50;"
      >
        <l-painter-text
          text="4399"
          css=" color: #fff;font-size: 50rpx;right: 93rpx;z-index: 50;"
        />
      </l-painter-view>
    </l-painter>

    <view @longpress="handleDownload(path)">
      <image :src="path" mode="widthFix" class="img"></image
    ></view>
  </view>
</template>
<script>
export default {
  components: {},
  data() {
    return {
      userId: 1,
      path: "",
    };
  },
  async onLoad() {},
  methods: {
    //长按下载
    handleDownload(url, type) {
      let _that = this;
      uni.getSetting({
        success(res) {
          if (!res.authSetting["scope.writePhotosAlbum"]) {
            uni.authorize({
              scope: "scope.writePhotosAlbum",
              success() {
                _that.savaToLocal(url, type);
              },
              fail() {
                uni.showToast({
                  title: "授权失败",
                  duration: 2000,
                });
              },
            });
          } else {
            _that.savaToLocal(url, type);
          }
        },
      });
    },

    //保存至本地
    savaToLocal(url, type) {
      uni.downloadFile({
        url: path,
        success: (res) => {
          if (res.statusCode === 200) {
            if (type == "img") {
              uni.saveImageToPhotosAlbum({
                filePath: res.tempFilePath,
                success: function () {
                  uni.showToast({
                    title: "保存成功",
                    duration: 2000,
                  });
                },
                fail: function (err) {
                  console.log("fail============", err);
                  uni.showToast({
                    title: "保存失败",
                    duration: 2000,
                  });
                },
              });
            } else {
              uni.saveVideoToPhotosAlbum({
                filePath: res.tempFilePath,
                success: function () {
                  uni.showToast({
                    title: "保存成功",
                    duration: 2000,
                  });
                },
                fail: function (err) {
                  console.log("fail============", err);
                  uni.showToast({
                    title: "保存失败",
                    duration: 2000,
                  });
                },
              });
            }
          }
        },
        fail(err) {
          console.log("fail==========", err);
          uni.showToast({
            title: "保存失败",
            duration: 2000,
          });
        },
      });
    },
  },
};
</script>

<style lang="scss">
.demo {
  position: relative;
}

.img {
  width: 100%;

  //   position: absolute;
  //   top: 300rpx;
  //   background-color: #fff;
  //   box-sizing: border-box;
  //   padding: 10rpx;
  //   border-radius: 10rpx;
  //   right: 100rpx;
  //   z-index: 50;
}
</style>
